<?php

namespace Analysis\Model;

use Think\Model;

class ApplyextModel extends Model
{
    protected $autoCheckFields = false;
    private $mob;
    private $val;
    private $exp;

    /** 账户信息 * */
    private $isbn;
    private $person;

    public function getSalesInfo($val, $mobile,$exp="")
    {

        $this->mob = $mobile;
        $this->exp=strtoupper($exp) == "NULL"?"":explode("+", $exp);
        $this->val = explode("+", $val);

        return $this->checkForRule();
    }

    private function checkForRule()
    {

        if (!$this->getMsgValLength()) {
            return $this->sendMessTo("您所发送的格式有误,请重新发送.");
        }
        if (count($this->val) == 3) {
            return $this->dealRegInfo();
        }
        if (count($this->val) == 4) {
            return $this->dealFreshInfo();
        }

        return $this->sendMessTo("系统错误");
    }

    private function dealRegInfo()
    {
        if (!$this->getTypeStatus()) {
            return $this->sendMessTo("您的申请代码错误,请检查后重新发送.");
        }
        if (!$this->getTwoMobileMatch()) {
            return $this->sendMessTo("您发送短信的手机号码与您要绑定的手机号码不一致哦！请用要绑定的手机号码再次发送短信");
        }
        if ($this->getUserSaleCode()) {
            return $this->sendMessTo("您已经申请过销售码了,无需再次申请.");
        }
        $exten = D("Exten");
        return $this->sendMessTo($exten->switchToDeal($this->val));
    }

    private function dealFreshInfo()
    {

        if (!$this->getCodeExists()) {
            return $this->sendMessTo("您的条码不存在.");
        }
        if (!$this->getUserExists()) {
            return $this->sendMessTo("您的账户不存在.");
        }
        if (!$this->getTwoMobileMatch()) {
            return $this->sendMessTo("您发送短信的手机号码与您要绑定的手机号码不一致哦！请用要绑定的手机号码再次发送短信");
        }

        return $this->freshUserData();
    }

    /**
     * 检测内容格式
     */
    private function getMsgValLength()
    {
        $length = count($this->val);
        return ($length != 3 && $length != 4) ? false : true;
    }

    /**
     * 检查当前类型是否使用
     */
    private function getTypeStatus()
    {
        $saletype = M("saletype");
        $info = $saletype->where("sat_status=1 and sat_para=" . $this->val[0])->find();
        return $info["sat_status"] == 1 ? true : false;
    }

    /**
     * 检查发送号码与接收号码是否一致
     */
    private function getTwoMobileMatch()
    {
        if (count($this->val) == 3) {
            return $this->mob == $this->val[2] ? true : false;
        } else {
            return $this->mob == $this->person["ssa_phone"] ? true : false;
        }
    }

    /**
     * 检测用户是否拥有优惠码
     */
    private function getUserSaleCode()
    {
        $salestaff = M("salestaff");
        $disisbn = M("disisbn");
        $saleagent = M("saleagent");

        $where["ssa_phone"] = $this->mob;
        $info = $salestaff->where($where)->find();
        $get["dis_ssaid"] = $info["ssa_id"];
        $code = $disisbn->where($get)->count();
        if ($this->val[0] == "2301" || $this->val[0] == "2302") {
            $agent = $saleagent->where("sag_phone='" . $this->mob . "'")->count();
            return $agent > 0 ? true : false;
        }
        return $code > 0 ? true : false;
    }

    /**
     * 检测优惠码是否存在
     */
    private function getCodeExists()
    {
        $disisbn = M("disisbn");
        $where["dis_isbn"] = $this->val[0];
        $this->isbn = $disisbn->where($where)->find();
        return $this->isbn["dis_id"] != null ? true : false;
    }

    /**
     * 检测用户是否存在
     */
    private function getUserExists()
    {
        $salestaff = M("salestaff");
        $where["ssa_phone"] = $this->mob;
        $this->person = $salestaff->where($where)->find();
        return $this->person["ssa_id"] != null ? true : false;
    }

    /**
     * 通用返回包
     */
    private function sendMessTo($val)
    {

        $info = D('Textchel')->getSmsParam($this->mob, $val);
        $status = explode(",", $info);
        $status=0;
        return $status == 0 ? true : false;
    }

    /**
     * 更新业务
     */
    private function freshUserData()
    {


        $salestaff = M("salestaff");
        $check = $this->person["ssa_bankname"] == $this->val["2"] ? 1 : 0;
        $check = $this->person["ssa_banktrans"] == $this->val["1"] ? $check + 1 : $check;
        $check = $this->person["ssa_banknumber"] == $this->val["3"] ? $check + 1 : $check;
        if ($check == 3) {
            return $this->sendMessTo("您的信息无需更新,如有疑问请联系客服人员.");
        }

        $where["ssa_id"] = $this->isbn["dis_ssaid"];

        $sav = array("ssa_bankname" => $this->val[2], "ssa_banktrans" => $this->val[1], "ssa_banknumber" => $this->val[3]);

        if(!empty($this->exp)){
            $sav = array_merge($sav,array(
                "ssa_name" =>$this->exp[0],
                "ssa_addr" =>$this->exp[9],
                "ssa_age" =>$this->exp[1],
                "ssa_alipay_account" =>$this->exp[8],
                "ssa_wechat_account" =>$this->exp[7],
                "ssa_created_at" =>$this->exp[10],
                "ssa_expired_at" =>$this->exp[11],
            ));
        }


        if ($salestaff->where($where)->save($sav)) {
            $val = $this->person['ssa_name'] . "+" . $this->person['ssa_phone'] . "你好，你的优惠码编号是" . $this->val[0];
            $val.="，绑定收款账号是" . $this->val[1] . "+" . $this->val[2] . "+" . $this->val[3];
            return $this->sendMessTo($val);
        } else {
            return $this->sendMessTo("系统错误.");
        }
    }
}
